<?php
/**
 * Description: 更新用户信息
 * User: devforma
 * Date: 15/1/6
 * Time: 11:05
 */
class UserInfoUpdate implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $uddb;
        
        //参数校验
        if (validateUser($queryArgs)!=1 || empty($queryArgs['info_type']) || empty($queryArgs['info_value'])) {
            return genOutput(1000);
        }

        switch ($queryArgs['info_type']) {
            case 'nickname':
                $update = array('nickname' => urldecode($queryArgs['info_value']));
                break;

            case 'avatar':
                $update = array('avatar' => urldecode($queryArgs['info_value']));
                break;
            
            case 'sex':
                $sex = urldecode($queryArgs['info_value']);
                $update = array('sex' => $sex=='mem' ? 1 : 0);
                break;

            case 'birthday':
                $update = array('birthday' => urldecode($queryArgs['info_value']));
                break;

            case 'address':
                // 此处用于修正js.encodeURIComponent()在某些平台浏览器下的异常处理
                $char = substr($queryArgs['info_value'], 0, 1);
                $count = substr_count($queryArgs['info_value'], '-');
                if ($char == '-' && $count > 5) {
                    $queryArgs['info_value'] = str_replace('-', '%', $queryArgs['info_value']);
                }

                $update = array('address' => urldecode($queryArgs['info_value']));
                break;

            case 'qq':
                $update = array('qq' => $queryArgs['info_value']);
                break;

            case 'alipay_id':
                $update = array('alipay_id' => $queryArgs['info_value']);
                break;

            case 'alipay_name':
                $update = array('alipay_name' => $queryArgs['info_value']);
                break;

            default:
                return genOutput(1000);
        }

        $uddb->update($uddb->userSocialInfo, $update, array('user_id' => $queryArgs['user_id']));

        //删除用户信息缓存
        $cacheKey = 'userInfo:'.$queryArgs['user_id'];
        cacheDelete($cacheKey);

        return genOutput(1);
    }
}